package ru.aptu.gsp_banking.dao_impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import ru.aptu.gsp_banking.dao.TransactionDAO;
import ru.aptu.gsp_banking.domain.Transaction;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 11/23/11
 * Time: 6:02 PM
 */

@Repository
public class TransactionDAOImpl implements TransactionDAO {

    @Autowired
	private SessionFactory sessionFactory;

    public List<Transaction> listTransaction(Integer accountId) {
        return (List<Transaction>) sessionFactory.getCurrentSession().
                createQuery("from Transaction where account_id=" + accountId.toString()).list();
    }

    public boolean checkTransaction(Integer transactionId, String name) {
        Transaction queryResult = (Transaction)sessionFactory.getCurrentSession().createQuery("from Transaction where id=" + Integer.toString(transactionId) +
                " and accountId in (select id from Account where username='" + name + "')").uniqueResult();
        return (queryResult != null);
    }

    public void saveTransaction(Transaction transaction) {
        sessionFactory.getCurrentSession().save(transaction);
    }

    public void deleteTransaction(Transaction transaction) {
		if (transaction != null) {
			sessionFactory.getCurrentSession().delete(transaction);
		}
    }
}
